import TableLyout from '@/components/commonPublic/tableLyout.vue'
import TableData from "@/components/commonPublic/el-table"; //主页面列表

import {
    getUserCompanyTreeList, //树状图
    getWorkplanInformationPageLists, //列表页接口


    submitPierProject,
    deletePierProjectByFid,

} from "@/api/request.js";
import publicForm from "@/components/QualityGridManage/fromCom.vue"; //弹框form
import tree from "@/components/BehaViorManage/tree"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import CustomDialog from "@/components/BehaViorManage/workYear/index.vue";

// import RingEcharts1 from "@/components/public/project"; //建设项目进场材料外委情况动态统计
// import peiNum from "@/components/public/peiNum"; //建设项目进场材料外委情况动态统计
// import pieStatitic from "@/components/public/pieStatitic"; //建设项目进场材料外委情况动态统计
import formJs from "./regexDetail.js"; //新增弹框配置项

// import engPage from "@/components/PCManage/engPage"; //建设项目进场材料外委情况动态统计

export default {
    name: "departmentWork",
    data() {
        return {
            name: "departmentWork",
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            checked: true, //查看下级按钮
            showName: "未选择公司",
            projectId: null,
            projectName: null,
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            checkData: [], //树状图默认选中id
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            isShow: false, //是否显示弹框
            status: null, //按钮状态1新增 3编辑
            statusNum: null,
            dataFid: null, //列表详情id
            currentRow: null, //列表详情

            // btnObj: [{
            //     visible: true, // 是否展示出来
            //     title: "新增",
            //     name: this.yj_edit,
            //     order: 0
            // }, {
            //     visible: true, // 是否展示出来
            //     title: "编辑",
            //     name: this.yj_edit,
            //     order: 1
            // }, {
            //     visible: true, // 是否展示出来
            //     title: "提交",
            //     name: "yj_submit",
            //     order: 2
            // }, {
            //     visible: true, // 是否展示出来
            //     title: "删除",
            //     name: "yj_delete",
            //     order: 3
            // }, {
            //     visible: true, // 是否展示出来
            //     title: "查看",
            //     name: "yj_viewForm",
            //     order: 4,
            // }],

            // 列表
            getList: {
                fCompanyid: "", //分子公司或建设项目id
                keyType: "", //计划类型
                fStatus: "", //完成情况(待审核,执行中,已打回,已完成)
                companyid: "", //项目部id
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                modelType: "2", // 0-待办 1-已办 传2 表示列表页查询
                fQualitygridclass: "",
                fMonth: "", //月 默认当前
                fYear: "", //年 默认当前
            },




            fFileName: "",
            pdfUrl: "",
            fPhotofilelist: [], //检测记录表文件集合
            samllTable: [], //检测任务信息列表
            picsfile: [], //现场照片
            exceptionfiles: [], //异常资料
            checkfiles: [], //检测资料资料
            handlerfiles: [], //处理照片
            lrCheckPiertask: {},
            checkFile: [], //本次检测附件资料信息
            lrCheckPierfoundation: {}, // 基桩检测表格信息集合
            activeIndex: "1",
            dis: true,



            fEquipmentname: [], //已选主要仪器设备

            dialogText: "",

            equipmentDetailId: null, //列表页id
            equipmentDetailIds: null, //检测任务信息列表页id

            sample: "",

            fId: null, //当前数据fId
            loading: false,
            dialogTableVisible: false,
            formLabelWidth: "150px",

            tableData: [],
            companyId: null,
            loadingDialog: false,
            currentRowId: "",

        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        elBreadcrumb,
        CustomDialog
        // engPage

    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));

        this.projectId = this.userInfo.projectId
        this.projectName = this.userInfo.projectId
        this.getList.userId = this.userInfo.projectName



        if (JSON.parse(sessionStorage.getItem("BehaViorManage_getList"))) {
            var dataList = JSON.parse(sessionStorage.getItem("BehaViorManage_getList"));
            this.getList = {
                fCompanyid: dataList.fCompanyid, // 施工标段id，不为空
                keyType: dataList.keyType, //计划类型
                fStatus: dataList.fStatus, //完成情况(待审核,执行中,已打回,已完成)
                companyid: "",
                page: 1, // 当前页数
                pageSize: 20, // 每页显示条数
                userId: "",
                modelType: 2,
                fQualitygridclass: dataList.fQualitygridclass,
                fYear: dataList.fYear,
                fMonth: dataList.fMonth
            };
        }

        if (JSON.parse(sessionStorage.getItem("BehaViorManage_treeCheck"))) {
            var data = JSON.parse(sessionStorage.getItem("BehaViorManage_treeCheck"));
            this.checkData = [data.fCompanyid];
        }
        this.getTree();
        this.btnRole(this.name)
    },
    methods: {
        //查看下级
        see() {
            if (this.checked == true) {
                this.getList.sublevel = 1;
            } else {
                this.getList.sublevel = 0;
            }
            this.getDataList(this.getList);
        },
        closeDialog() {
            this.isShow = false;

        },
        // 左侧树结构
        getTree() {
            this.treeText.companyId = this.userInfo.companyId;
            this.treeText.userId = this.userInfo.userId;
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("BehaViorManage_treeCheck")
                            ).fShortname;
                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }
                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {

            this.showName = data.fShortname; //顶部显示机构名

            this.getList.fCompanyid = data.fCompanyid;

            this.getList.page = 1;
            this.getList.pageSize = 20;

            // this.yj_searchForm(this.getList);
            this.getList = {
                fCompanyid: data.fCompanyid, // 左侧选中组织机构id
                keyType: "", //计划类型
                fStatus: "", //完成情况(待审核,执行中,已打回,已完成)
                companyid: "", //项目部id
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
                modelType: "2" // 0-待办 1-已办 传2 表示列表页查询


                // "fCompanyid": "",
                // "keyType": "",
                // "fStatus": "",
                // "companyid": "",
                // "page": 1,
                // "pageSize": 5,
                // "userId": "445ecd6f-c599-432d-9d2e-fb56ba447117",
                // "modelType": "2"
            };
            this.yj_searchForm(this.getList);
        },
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = [{
                    fEncode: "yj_view",
                    fFullname: "查看",
                    fOrder: 1
                }]
            }
        },
        getBtnName(name, status) {
            this[name](status)
        },

        // 新增/编辑
        yj_edit(num) {
            if (num == 0) {
                // 新增
                if (this.getList.fUserid != "System") {
                 
                    // this.$router.push({
                    //     path: "/pcManage/engineeringDetail",
                    //     query: {
                    //         data: num,
                    //         fCompanyid: this.falgId,
                    //     },
                    // });
                    // this.formData = this.$options.data.call(this).formData

                    //     this.timestamp1 = "EQUIP" + Date.parse(new Date());
                    //     this.formData.fDesignfile = this.timestamp1;
                    //     this.formData.userId = this.userInfo.userId;
                    //     this.formData.userName = this.userInfo.realName;
                    //     this.formData.fRegistrantid = this.userInfo.userId;
                    //     this.formData.fRegistrantname = this.userInfo.realName;
                    //     this.dialogText = "新增";
                    //     this.fPhotofilelist = [];
                    //     this.dialogTableVisibleText = true;
                } else {
                    this.$message.warning("该用户无法新增");
                }
            } else {
                // 编辑
                this.editText();
            }
        },

        // 提交
        SubmitText(id) {
            if (id != "" && id != null) {
                this.$confirm("是否继续提交?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                    .then(() => {
                        this.loading = true;
                        submitPierProject({
                                fId: id
                            })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.loading = false;
                                    this.getDataList(this.getList);
                                    this.$message.success("提交成功");
                                } else {
                                    this.$message.error("提交失败", res.msg);
                                    this.loading = false;
                                    return false;
                                }
                            })
                            .catch(() => {
                                this.loading = false;
                                console.log("提交失败");
                            });
                    })
                    .catch(() => {
                        this.$message("取消提交");
                    });
            } else {
                return this.$message.error("请先选择需要提交的数据");
            }
        },
        // 删除
        rowDelete(id) {
            if (id != "" && id != null) {
                this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                    .then(() => {
                        this.loading = true;
                        deletePierProjectByFid({
                                fId: id
                            })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.loading = false;
                                    this.getDataList(this.getList);
                                    this.$message.success("删除成功");
                                } else {
                                    this.$message.error("删除失败", res.msg);
                                    this.loading = false;
                                }
                            })
                            .catch(() => {
                                console.log("删除失败");
                                this.loading = false;
                            });
                    })
                    .catch(() => {
                        this.$message("取消删除");
                    });
            } else {
                return this.$message.error("请先选择需要删除的数据");
            }
        },


        // 点击查看
        yj_viewForm(row, label) {

            if (label == "完成状态") {
                this.currentRow = row;

                this.statusNum = 7
                this.isShow = true;
                if (row.fStatuss == '编辑中') {
                    this.status = [true, false, false, false]
                } else if (row.fStatuss == '待审核') {
                    this.status = [true, false, false, false]
                } else if (row.fStatuss == '执行中') {
                    this.status = [true, true, false, false]
                } else if (row.fStatuss == '审核中') {
                    this.status = [true, true, true, false]
                } else if (row.fStatuss == '已完成') {
                    this.status = [true, true, true, true]
                } else if (row.fStatuss == '审核未通过') {
                    this.status = [true, true, false, false]
                } else if (row.fStatuss == '已打回') {
                    this.status = [true, true, true, false]
                }
            }
        },


        // 点击当前行变色

        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
            var status = this.currentRow //当前数据状态
            if (status != null && status.fStatuss == '编辑中') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[2]['disabled'] = false //编辑
                this.btnObj[3]['disabled'] = false //删除
                this.btnObj[4]['disabled'] = false //提交

            } else if (status != null && status.fStatuss == '待审核') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[5]['disabled'] = false //审核

            } else if (status != null && status.fStatuss == '审核未通过') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[3]['disabled'] = false //删除

            } else if (status != null && status.fStatuss == '执行中') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[1]['disabled'] = false //完成情况
                this.btnObj[4]['disabled'] = false //提交

            } else if (status != null && status.fStatuss == '审核中') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[5]['disabled'] = false //审核
            } else if (status != null && status.fStatuss == '已打回') {
                this.btnObj.map(item => {
                    item['disabled'] = true
                })
                this.btnObj[0]['disabled'] = false //新增
                this.btnObj[1]['disabled'] = false //完成情况
                this.btnObj[4]['disabled'] = false //提交



            }
        },


        // 获取列表数据
        yj_searchForm(data) {
            this.loading = true;
            var that = this;

            window.sessionStorage.setItem(
                "BehaViorManage_getList",
                JSON.stringify(data)
            );

            var obj = {
                fCompanyid: data.fCompanyid, //分子公司或建设项目id
                keyType: data.keyType, //计划类型
                fStatus: data.fStatus, //完成情况(待审核,执行中,已打回,已完成)
                companyid: "", //项目部id
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
                modelType: "2", //, 0-待办 1-已办 传2 表示列表页查询
                stationType: "1" //分子公司与项目部区分标识 0是分子公司 1是项目部
            }
            getWorkplanInformationPageLists(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                      
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        // 下面图表

        // 点击当前行
        getRowInfo(e) {
            this.equipmentDetailId = e.F_Id;
        },

        // 点击报告编码
        openDialog(row) {
            this.equipmentDetailId = row.F_Id;
            this.edit(1);
            this.getPierProjectTaskLists();
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
    },
};